package cn.edu.cimv.controller;

import cn.edu.cimv.constant.InfoConstant;
import cn.edu.cimv.dao.entity.Cata;
import cn.edu.cimv.dao.entity.Menu;
import cn.edu.cimv.pojo.Message;
import cn.edu.cimv.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * Created by 门铎 on 2016/6/24.
 */
@Controller
@RequestMapping("/cimv/single")
public class ContentController {
    @Autowired private ContentService contentService;
    @Autowired private Message message;
    @RequestMapping("{name}")
   public String single(@PathVariable("name")String name, Model model){
        //页面显示的信息
        Menu menu = contentService.getMenu(name);
        model.addAttribute("menu",menu);

        int count = contentService.checkCataIsExisting(name);

        if(count == 0){
            return "single/new";
        }else{
            Cata cata = contentService.getCata(name);
            model.addAttribute("cata",cata);
            return "single/update";
        }
    }
    @RequestMapping(value = "{name}/new",method = RequestMethod.POST)
    public @ResponseBody Message singleNew(@PathVariable("name")String name,
                      @RequestParam("editor")String content){
        contentService.newCata(name,content);
        message.setMsg(InfoConstant.CREATE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "{name}/update",method = RequestMethod.POST)
    public @ResponseBody Message singleU(@PathVariable("name")String name,
                                           @RequestParam("editor")String content){
        contentService.updateCata(name,content);
        message.setMsg(InfoConstant.UPDATE_NORMAL_SUCCESS);
        return message;
    }
}
